import { ViewTool } from '@/utils';

<#ibizinclude>
../@MACRO/MODEL/MODEL_HEADER.ts.ftl
</#ibizinclude>

    /**
     * 菜单项集合
     *
     * @private
     * @type {any[]}
     * @memberof ${srfclassname(ctrl.codeName)}Model
     */
    private items: any[] = [
<@ibizindent blank=8>
        <#list ctrl.getPSAppMenuItems() as child>
        ${P.getPartCode(child,"ITEM").code},
        </#list>
</@ibizindent>
    ];

	/**
	 * 应用功能集合
	 *
	 * @private
	 * @type {any[]}
	 * @memberof ${srfclassname(ctrl.codeName)}Model
	 */
	private funcs: any[] = [
        <#if view.isDefaultPage?? && view.isDefaultPage()>
        <#list ctrl.getPSAppFuncs() as appFuncs>
        {
            appfunctag: '${appFuncs.getCodeName()}',
            appfuncyype: '${appFuncs.getAppFuncType()}',
            <#if appFuncs.getAppFuncType() == 'APPVIEW'>
            <#assign dataview = appFuncs.getPSAppView()/>
            openmode: '${dataview.getOpenMode()}',
            codename: '${dataview.getCodeName()?lower_case}',
            deResParameters: [],
            <#--  BEGIN：是否应用实体视图  -->
            <#if dataview.isPSDEView()>
            <#assign appDataEntity = dataview.getPSAppDataEntity()/>
            routepath: '/${view.getCodeName()?lower_case}/:${view.getCodeName()?lower_case}?/${srfpluralize(appDataEntity.codeName)?lower_case}/:${appDataEntity.getCodeName()?lower_case}?/${dataview.getPSDEViewCodeName()?lower_case}/:${dataview.getPSDEViewCodeName()?lower_case}?',
            parameters: [
                { pathName: '${srfpluralize(appDataEntity.codeName)?lower_case}', parameterName: '${appDataEntity.getCodeName()?lower_case}' },
                { pathName: '${dataview.getPSDEViewCodeName()?lower_case}', parameterName: '${dataview.getPSDEViewCodeName()?lower_case}' },
            ],
            <#else>
            routepath: '/${view.getCodeName()?lower_case}/:${view.getCodeName()?lower_case}?/${dataview.getCodeName()?lower_case}/:${dataview.getCodeName()?lower_case}?',
            parameters: [
                { pathName: '${dataview.getCodeName()?lower_case}', parameterName: '${dataview.getCodeName()?lower_case}' },
            ],
            </#if>
            <#--  END：是否应用实体视图  -->
            </#if>
        },
        </#list>
        </#if>
	];

	/**
	 * 根据当前路由查找激活菜单
	 *
	 * @param {*} route
	 * @returns {*}
	 * @memberof ${srfclassname(ctrl.codeName)}Model
	 */
	public findActiveMenuByRoute(route: any): any {
		if (route) {
			const func = this.funcs.find((item: any) => {
				if (item.openmode === '') {
					const url: string = ViewTool.buildUpRoutePath(route, route.params, [], item.parameters, [], {});
					return url === route.fullPath;
				}
			});
            if (func) {
			    return this.findMenuByFuncTag(func.appfunctag);
            }
		}
	}

	/**
	 * 根据应用功能id查找菜单项
	 *
	 * @param {string} tag
	 * @param {any[]} [menus=this.items]
	 * @returns {*}
	 * @memberof ${srfclassname(ctrl.codeName)}Model
	 */
	public findMenuByFuncTag(tag: string, menus: any[] = this.items): any {
		let menu: any;
		menus.every((item: any) => {
			if (item.appfunctag === tag) {
				menu = item;
				return false;
			}
			if (item.items) {
				menu = this.findMenuByFuncTag(tag, item.items);
				if (menu) {
					return false;
				}
			}
			return true;
		});
		return menu;
	}

	/**
	 * 查找默认打开菜单
	 *
	 * @param {any[]} [menus=this.items]
	 * @returns {*}
	 * @memberof ${srfclassname(ctrl.codeName)}Model
	 */
	public findDefaultOpenMenu(menus: any[] = this.items): any {
		let menu: any;
		menus.every((item: any) => {
			if (item.opendefault === true) {
				menu = item;
				return false;
			}
			if (item.items) {
				menu = this.findMenuByFuncTag(item.items);
				if (menu) {
					return false;
				}
			}
			return true;
		});
		return menu;
	}

    /**
     * 获取所有菜单项集合
     *
     * @returns {any[]}
     * @memberof ${srfclassname(ctrl.codeName)}Model
     */
    public getAppMenuItems(): any[] {
        return this.items;
    }

	/**
	 * 根据名称获取菜单组
	 *
	 * @param {string} name
	 * @returns {*}
	 * @memberof ${srfclassname(ctrl.codeName)}Model
	 */
	public getMenuGroup(name: string): any {
		return this.items.find((item: any) => Object.is(item.name, name));
	}

    /**
     * 获取所有应用功能集合
     *
     * @returns {any[]}
     * @memberof ${srfclassname(ctrl.codeName)}Model
     */
    public getAppFuncs(): any[] {
        return this.funcs;
    }
<#ibizinclude>
../@MACRO/MODEL/MODEL_BOTTOM.ts.ftl
</#ibizinclude>